#include "stdafx.h"
#include <QLineEdit>
#include <QDateEdit>
#include <QCheckBox>
#include <QSpinBox>

#include "TaskTreeDelegate.h"
//#include "ProjectPlan/PPTask.h"

namespace LBPROJLOGIC
{
    CTaskTreeDelegate::CTaskTreeDelegate(QObject *parent)
        : QStyledItemDelegate(parent)
    {
    }


    CTaskTreeDelegate::~CTaskTreeDelegate()
    {
    }

    QWidget * CTaskTreeDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
        QWidget* pEditorWidget = nullptr;
        switch (index.column())
        {
        case TC_NAME:
        case TC_PRE_TASK:
            pEditorWidget = new QLineEdit(parent);
            break;
        case TC_SPAN:
        case TC_ACTRUAL_SPAN:
            pEditorWidget = new QSpinBox(parent);
            break;
        case TC_START:
        case TC_END:
        case TC_ACTRUAL_START:
        case TC_ACTRUAL_END:
            pEditorWidget = new QDateEdit(parent);
            break;
        case TC_MILESTONE:
            pEditorWidget = new QCheckBox(parent);
            break;
        default:
            break;
        }
        return pEditorWidget;
    }

    void CTaskTreeDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
    {
        QVariant value = index.model()->data(index, Qt::EditRole);
        switch (index.column())
        {
        case TC_NAME:
        case TC_PRE_TASK:
        {
            QLineEdit* pEdit = dynamic_cast<QLineEdit*>(editor);
            pEdit->setText(value.toString());
        }
        break;
        case TC_SPAN:
        case TC_ACTRUAL_SPAN:
        {
            QSpinBox* pEdit = dynamic_cast<QSpinBox*>(editor);
            pEdit->setValue(value.toInt());
        }
        break;
        case TC_START:
        case TC_END:
        case TC_ACTRUAL_START:
        case TC_ACTRUAL_END:
        {
            QDateEdit* pEdit = dynamic_cast<QDateEdit*>(editor);
            pEdit->setDateTime(value.toDateTime());
            pEdit->setCalendarPopup(true);
        }
        break;
        case TC_MILESTONE:
        {
            QCheckBox* pCheckBox = dynamic_cast<QCheckBox*>(editor);
            pCheckBox->setChecked(value.toBool());
        }
        break;
        }
    }

    void CTaskTreeDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    {
        switch (index.column())
        {
        case TC_NAME:
        case TC_PRE_TASK:
        {
            QLineEdit* pEdit = dynamic_cast<QLineEdit*>(editor);
            QString value = pEdit->text();
            model->setData(index, value, Qt::EditRole);
        }
        break;
        case TC_SPAN:
        case TC_ACTRUAL_SPAN:
        {
            QSpinBox* pEdit = dynamic_cast<QSpinBox*>(editor);
            int value = pEdit->value();
            model->setData(index, value, Qt::EditRole);
        }
        break;
        case TC_START:
        case TC_END:
        case TC_ACTRUAL_START:
        case TC_ACTRUAL_END:
        {
            QDateEdit* pEdit = dynamic_cast<QDateEdit*>(editor);
            model->setData(index, pEdit->dateTime(), Qt::EditRole);
        }
        break;
        case TC_MILESTONE:
        {
            QCheckBox* pCheckBox = dynamic_cast<QCheckBox*>(editor);
            model->setData(index, pCheckBox->isChecked(), Qt::EditRole);
        }
        break;
        }

    }

    void CTaskTreeDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
        editor->setGeometry(option.rect);
    }
}
